
/*   RolGPS Project
 * 
 *   Copyright (C) 2009 Pablo Alejandro Costesich <rhfixer@gmail.com>
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package org.rolgps.libnstack.message;

import java.util.ArrayList;
import java.util.List;
import org.rolgps.libnstack.protocol.Message;

/**
 *
 * @author xeph
 */
public class GenericMessage implements Message {

    List<String[]> headers = new ArrayList();
    List<String[]> bodyPairs = new ArrayList();
    String name = null;

    /**
     *
     * @param name The name of the message sender.
     * @returns an empty GenericMessage.
     *
     */
    public GenericMessage(String name) {
        this.name = name;
    }

    /**
     *
     */
    public GenericMessage() {
        this.name = "noname";
    }

    /**
     *
     * @param list
     * @param key
     * @param value
     * @return
     */
    private boolean checkExistance(List<String[]> list, String key, String value){
        boolean result = false;
        for (String[] pair : list) {
            if (pair[0].equals(key) && pair[1].equals(value)) {
                result = true;
            }
        }
        return result;
    }
    /**
     *
     * @param list
     * @param key
     * @return
     */
    private String[] getValueFromList(List<String[]> list, String key){
        String[] result = new String[0];
        @SuppressWarnings("unchecked")
        List<String> auxResult = new ArrayList<String>();
        for (String[] pair : list){
            if(pair[0].equals(key)){
                auxResult.add(pair[1]);
            }
        }

        result= auxResult.toArray(new String[auxResult.size()]);
        return result;
    }
    /**
     *
     * @param service
     */
    public void sendTo(String service) {
        this.addHeader("sendTo", service);
    }

    /**
     *
     * @param services
     */
    public void sendTo(String[] services) {
        for (String service : services) {
            this.sendTo(service);
        }
    }

    /**
     *
     * @return
     */
    public String from() {
        return name;
    }

    /**
     *
     * @param key
     * @param value
     */
    public void addHeader(String key, String value) {
        if (!checkExistance(headers, key, value)) {
            headers.add(new String[]{key, value});
        }
    }

    /**
     *
     * @return
     */
    public List getHeaders() {
        return headers;
    }


    /**
     *
     * @param key
     * @param value
     */
    public void addTuple(String key, String value) {
        if (!checkExistance(bodyPairs, key, value)) {
            bodyPairs.add(new String[]{key, value});
        }
    }

    /**
     *
     * @return
     */
    public List getTuples() {
        return bodyPairs;
    }

    /**
     *
     * @param key
     * @return
     */
    public String[] getHeader(String key) {
        return getValueFromList(headers, key);
    }

    /**
     * 
     * @param key
     * @return
     */
    public String[] getTuple(String key) {
        return getValueFromList(bodyPairs, key);
    }

    public void from(String s) {
        this.name=s;
    }
}
